// Copyright Epic Games, Inc. All Rights Reserved.

#include "class03GameModeBase.h"
#include <Engine/StaticMeshActor.h>
#include <Camera/PlayerCameraManager.h>
#include <Camera/CameraActor.h>
#include <EngineUtils.h>

void Aclass03GameModeBase::StartPlay()
{
    // Get all actors in scene
    for (TActorIterator<AActor> itr(GetWorld()); itr; ++itr)
    {
        AStaticMeshActor* mesh = dynamic_cast<AStaticMeshActor*>(*itr);
        if (mesh)
        {
            FString loc = mesh->GetActorLocation().ToString();
            UE_LOG(LogTemp, Warning, TEXT("Static-mesh %s found at %s"), *(mesh->GetName()), *loc);
        }
        else
            UE_LOG(LogTemp, Warning, TEXT("Actor %s found"), *((*itr)->GetName()));
    }

    // Set a timer to update camera pose information
    GetWorldTimerManager().SetTimer(
        _camUpdateHandle, this, &Aclass03GameModeBase::AdvanceTimer, 1.0f, true);
    AGameModeBase::StartPlay();
}

void Aclass03GameModeBase::AdvanceTimer()
{
    // Get camera location and forward
    for (TActorIterator<APlayerCameraManager> itr(GetWorld()); itr; ++itr)
    {
        APlayerCameraManager* camManager = *itr;
        FVector location = camManager->GetCameraLocation();
        FVector forward = camManager->GetCameraRotation().Vector();

        FString poseString = location.ToString() + "; " + forward.ToString();
        UE_LOG(LogTemp, Warning, TEXT("Camera pose: %s"), *poseString);
    }

    for (TActorIterator<AStaticMeshActor> itr(GetWorld()); itr; ++itr)
    {
        AStaticMeshActor* mesh = *itr;
        if (!mesh->IsValidLowLevel()) return;
        else { mesh->Destroy(); return; }
    }
}
